package com.zyf.workflow.test.work.firmware.worker;

import com.zyf.workflow.business.fwupgrade.FwUpgradeBean;
import com.zyf.workflow.exception.ArrangeException;
import com.zyf.workflow.test.work.firmware.bean.FirmwareBean;
import com.zyf.workflow.test.work.firmware.bean.ResourceBean;

import java.util.List;


public class FirmwareUpgradeWorker {

    public String checkResourceState(FirmwareBean firmwareBean, String param) {
        System.out.println("check resource state start");
        String state = "checkBand";
        System.out.println("resoutce state is outband:" + state);

        return state;
    }


    public String checkBandType(FirmwareBean firmwareBean, String param) {
        System.out.println("check band type start");
        boolean outBand = true;
        List<FwUpgradeBean> fwUpgradeBeans = firmwareBean.getFwUpgradeBeans();
        for (FwUpgradeBean fwUpgradeBean : fwUpgradeBeans) {
            if ("nic".equals(fwUpgradeBean.getType()) || "raid".equals(fwUpgradeBean.getType())) {
                outBand = false;
                break;
            }
        }
        System.out.println("resoutce state is outband:" + outBand);

        return outBand ? "outband" : "inband";
    }


    public String configVisualMedia(FirmwareBean firmwareBean, String param) {
        String state = "success";
        if (!"centos".equals(firmwareBean.getIsoName())) {
            throw new ArrangeException("cant find iso file");
        }
        System.out.println("config visual media start");
        ResourceBean resourceBean = firmwareBean.getResourceBean();
        System.out.println("config visual media ip is:" + resourceBean.getIp());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return state;
    }

    public String configCDBoot(FirmwareBean firmwareBean, String param) {
        String state = "success";
        System.out.println("config " + firmwareBean.getResourceBean().getIp() + " boot from CDRoom");
        System.out.println("restart server " + firmwareBean.getResourceBean().getIp());
        return state;
    }

    public Void getProcess(FirmwareBean firmwareBean, String param) {
        List<FwUpgradeBean> fwUpgradeBeans = firmwareBean.getFwUpgradeBeans();
        for (FwUpgradeBean fwUpgradeBean : fwUpgradeBeans) {
            for (int i = 0; i < 10; i++) {
                int process = (i + 1) * 10;
                System.out.println("get firmware type:" + fwUpgradeBean.getType() + " process " + process + "%");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


}
